<template>
  <div class="drag" v-drag>
    <div class="drag-title">编辑导航项</div>
    <div @click="addItem" class="add-item">
      <div>+</div>
      <div>新建名称</div>
    </div>
    <div class="drag-content">
      <div class="head">导航项列表</div>
      <div class="item-box flex-sb flex-y-center title">
        <div>项名称</div>
        <div>排序</div>
        <div>操作</div>
      </div>
      <div class="scroll-box">
        <SlickList
          :lockToContainerEdges="true"
          axis="y"
          lockAxis="y"
          v-model="list"
          class="SortableList"
          @input="getChangeLists"
        >
          <SlickItem
            v-for="(item, index) in list"
            class="SortableItem"
            :index="index"
            :key="index"
          >
            <div class="item-box flex-sb flex-y-center">
              <div>
                <div
                  @mousedown.stop
                  v-show="!item.check"
                >
                  {{ item.name }}
                </div>
                <div @mousedown.stop>
                  <el-input
                    v-show="item.check"
                    v-model="item.name"
                    placeholder="请输入内容"
                    maxlength="4"
                    size="mini"
                    class="input"
                    @blur="nameBlur($event, index)"
                  ></el-input>
                </div>
              </div>
              <div class="icon-box">
                <i
                  v-show="index != list.length - 1"
                  @mousedown.stop
                  @click="itemSwitch('bottom', index)"
                  class="el-icon-sort-down"
                ></i>
                <i
                  v-show="index !== 0"
                  @mousedown.stop
                  @click="itemSwitch('top', index)"
                  class="el-icon-sort-up"
                ></i>
              </div>
              <div>
                <i
                  @mousedown.stop
                  @click="itemDelete(item, index)"
                  class="el-icon-delete"
                ></i>
              </div>
            </div>
          </SlickItem>
        </SlickList>
      </div>
    </div>
  </div>
</template>
<script >
import { SlickList, SlickItem } from "vue-slicksort";
import { cubeNav } from "@/components/DiyNew/diy/dataConfig/config.js";
import { userTools } from "@/components/DiyNew/diy/systemPages/userPage/dataConfig/config.js";

export default {
  props: {
    navList: {
      type: Array,
      default: () => {
        return [];
      },
    },
    type:{
      type:String,
      default:''
    }
  },
  components: {
    SlickList,
    SlickItem,
  },
  watch: {},
  computed: {
    list: {
      get() {
        return this.navList;
      },
      set(v) {
        console.log(v);
        this.$emit("change", v);
      },
    },
  },
  data() {
    return {
      flag: true,
      clickNum: 0,
    };
  },
  methods: {
    // 拖动位置变换
    getChangeLists(vals) {
      console.log(vals, "vals");
    },
    // 点击切换上下位置
    itemSwitch(type, index) {
      if (type == "top") {
        this.navList[index] = this.navList.splice(
          index - 1,
          1,
          this.navList[index]
        )[0];
      } else if (type == "bottom") {
        this.navList[index] = this.navList.splice(
          index + 1,
          1,
          this.navList[index]
        )[0];
      }
    },
    // 删除某一项
    itemDelete(item, index) {
      this.navList.splice(index, 1);
    },
    // 添加子项
    addItem() {
      if(this.type == 'userTools') {
        let item = JSON.parse(JSON.stringify(userTools.params.list[0]));
        this.navList.push(item);
      }
      if(this.type == 'cubeNav') {
        let item = JSON.parse(JSON.stringify(cubeNav.params.list[0]));
        this.navList.push(item);
      }
    },
    // 双击名称
    clickName(index) {
      console.log(index);
      this.navList.forEach((item) => {
        item.check = false;
      });
      let item = this.navList[index];
      item.check = true;
      this.navList = this.navList
    },
    // 输入框失去焦点
    nameBlur(e, index) {
      this.navList[index].name = e.target.value;
      this.navList.forEach((item) => {
        delete item.check;
      });
      this.navList = this.navList
    },
  },
  directives: {
    // 自定义拖拽
    drag(el) {
      let oDiv = el; //当前元素
      //禁止选择网页上的文字
      document.onselectstart = function () {
        return false;
      };
      oDiv.onmousedown = function (e) {
        //鼠标按下，计算当前元素距离可视区的距离
        let disX = e.clientX - oDiv.offsetLeft;
        let disY = e.clientY - oDiv.offsetTop;

        document.onmousemove = function (e) {
          //通过事件委托，计算移动的距离
          let l = e.clientX - disX;
          let t = e.clientY - disY;
          //移动当前元素
          oDiv.style.left = l + "px";
          oDiv.style.top = t + "px";
        };
        document.onmouseup = function (e) {
          document.onmousemove = null;
          document.onmouseup = null;
        };
        //return false不加的话可能导致黏连，就是拖到一个地方时div粘在鼠标上不下来，相当于onmouseup失效
        return false;
      };
    },
  },
};
</script>
<style lang="scss">
.input {
  input {
    padding: 0 6px;
  }
}
</style>
<style lang="scss" scoped>
.flex-sb {
  display: flex;
  justify-content: space-between;
}
.flex-y-center {
  display: flex;
  align-items: center;
}

.drag {
  z-index: 9999;
  position: absolute;
  left: 0;
  top: 50%;
  transform: translate(-102%, -50%);
  min-width: 220px;
  width: 220px;
  background: #fff;
  cursor: pointer;
  border-radius: 4px;
  box-shadow: 0 2px 6px rgba(0,0,0,.08);
  ::-webkit-scrollbar {
    width: 5px;
    height: 5px;
    /**/
  }
  ::-webkit-scrollbar-track {
    background: rgb(239, 239, 239);
    border-radius: 2px;
  }
  ::-webkit-scrollbar-thumb {
    background: #bfbfbf;
    border-radius: 10px;
  }
  ::-webkit-scrollbar-thumb:hover {
    background: #333;
  }
  ::-webkit-scrollbar-corner {
    background: #179a16;
  }
}
.drag-title {
  width: 100%;
  padding: 0 14px;
  background: #011528;
  color: #fff;
  font-size: 14px;
  line-height: 40px;
  border-radius: 4px 4px 0 0;
}
.add-item {
  display: flex;
  align-items: center;
  font-weight: bold;
  > div:nth-child(1) {
    margin-right: 10px;
  }
  padding: 0 14px;
  font-size: 12px;
  line-height: 40px;
  border-bottom: 1px solid #f0f2f5;
}
.add-item:hover {
  color: #1890fc;
}

.drag-content {
  padding: 0 14px 14px 14px;
  .scroll-box {
    max-height: 350px;
    overflow-y: auto;
  }
  .SortableItem {
    line-height: 32px;
  }
}
.item-box {
  font-size: 12px;
  > div {
    display: flex;
    justify-content: center;
    flex: 1;
  }
}
.title {
  background-color: #f6f7f9;
  padding: 5px 0 ;
}
.SortableItem {
  background-color: #f6f7f9;
  i {
    font-weight: bold;
  }
  i:hover {
    color: #1890fc;
  }
}
.SortableItem:hover {
  background-color: #eaf5ff;
}
.head {
  padding: 5px 0;
  font-size: 12px;
}
</style>
 